package com.wolfheader.wolfgame.dao.admin;

import javax.transaction.Transactional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import com.wolfheader.wolfgame.entity.GameRecordEntity;

@Transactional
public interface GameRecordDao extends PagingAndSortingRepository<GameRecordEntity, Long> {

	@Query("select r from GameRecordEntity r where r.gameid=:gameid and r.gamePlayer.openid=:openid")
	public GameRecordEntity findPlayerByOpenidAndGameid(@Param("openid")String openid, @Param("gameid")String gameid);

	
	/**
	 * 获取玩家指定游戏的排行
	 * 
	 * @return
	 */
	Page<GameRecordEntity> findListByGameid(Specification<GameRecordEntity> spec, Pageable pageable);

	
	/**
	 * 获取排行名次
	 * @param gameid
	 * @param openid
	 * @param score
	 * @return
	 */
	@Query("select count(r) from GameRecordEntity r where r.gameid=:gameid and r.totalScore>=:score and r.recordDateTime<=:dateTime")
	Integer computeMyRank(@Param("gameid") String gameid, @Param("score") Integer score, @Param("dateTime") Long dateTime);
	
	
	
	
	
}
